﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.Cache.CacheItemPolicyPackage
{
    internal class AbsoluteCacheItemPolicy : ICacheItemPolicy
    {
        private readonly int _seconds = 0;
        private readonly int _minutes = 0;
        private readonly int _hours = 0;
        private readonly DateTimeOffset _expireTime = DateTimeOffset.MinValue;

        internal AbsoluteCacheItemPolicy(DateTimeOffset expireTime)
        {
            _expireTime = expireTime;
        }

        internal AbsoluteCacheItemPolicy(int seconds)
        {
            _seconds = seconds;
        }

        internal AbsoluteCacheItemPolicy(int minutes, int seconds) : this(seconds)
        {
            _minutes = minutes;
        }

        internal AbsoluteCacheItemPolicy(int hours, int minutes, int seconds) : this(minutes, seconds)
        {
            _hours = hours;
        }

        CacheItemPolicy ICacheItemPolicy.GetCacheItemPolicy()
        {
            var expireTime = _expireTime == DateTimeOffset.MinValue
                ? DateTimeOffset.Now.AddHours(_hours).AddMinutes(_minutes).AddSeconds(_seconds)
                : _expireTime;
            return new CacheItemPolicy() { AbsoluteExpiration = expireTime };
        }
    }
}
